package cn.sccdlg.ssm.service.impl;

import cn.sccdlg.ssm.dao.AccountDao;
import cn.sccdlg.ssm.domain.Account;
import cn.sccdlg.ssm.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author RaoShuang
 * @emil 991207823@qq.com
 * @date 2020/10/24 23:11
 * 账户业务层实现类
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    public List<Account> findAll() {
        System.out.println("业务层：查询所有账户...");
        return accountDao.findAll();
    }

    public void saveAccount(Account account) {
        System.out.println("业务层：保存帐户...");
        accountDao.saveAccount(account);
    }

    @Override
    public void transfer(Integer sourceId, Integer targetId, Float money) {
        System.out.println("transfer....");
        //2.1根据名称查询转出账户
        Account source = accountDao.findOne(sourceId);
        //2.2根据名称查询转入账户
        Account target = accountDao.findOne(targetId);
        //2.3转出账户减钱
        source.setMoney(source.getMoney() - money);
        //2.4转入账户加钱
        target.setMoney(target.getMoney() + money);
        //2.5更新转出账户
        accountDao.updateAccount(source);
        //模拟发生异常
        int i = 1 / 0;
        //2.6更新转入账户
        accountDao.updateAccount(target);
    }
}
